﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Mvc.Filters;
using System.Web;
using System.Web.Routing;
using MvcEngine.Blog.Models.Entities;
using MvcEngine.Core;
using MvcEngine.Blog.Repositories;
using MvcEngine.Blog.Managers;

namespace MvcEngine.Blog.Components.Attributes
{
    public class PostActionSetupAttribute : PageHeaderAttribute
    {
        public override PageHeader Get(RequestContext requestContext)
        {
            BlogPost post = null;

            Guid? pageId = requestContext.RouteData.Values["pageId"] as Guid?;

            if (requestContext.RouteData.Values["postName"] != null && pageId.HasValue)
            {
                string postName = requestContext.RouteData.Values["postName"].ToString();
                var blog = BlogManager.Current.GetBlogByPage(pageId.Value);
                post = BlogPostManager.Current.GetPost(postName, blog.Id);
            }

            if (requestContext.RouteData.Values["postId"] != null)
            {
                Guid postId = new Guid(requestContext.RouteData.Values["postId"].ToString());
                post = BlogPostManager.Current.GetPost(postId);
            }


            if (post != null)
            {
                PageHeader header = new PageHeader()
                {
                    Title = post.Title,
                    Description = post.Description,
                    Keywords = post.Keywords
                };
                return header;
            }
            return null;
        }
    }
}
